 /*
 * Copyright (c) 2010, James Daniello
 * This project is licensed under the 'New BSD' license.
 * The full text of the license can be found in the root
 * directory under the filename "LICENSE"
 */
package src.gui;

import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Container;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

public class PICAIUClientGUINewGame implements PICAIUGUI {

  //The various gui components.
  private Container frame;       //<-- the main panel container
  private JPanel content;
  private PICAIUClientGUI parent;
  //gui elements
  private JLabel myInfo;
  private JPanel games, myInfoCont, buttonPanel, padding;
  private JList gamesList;                 //<-- the list of available game types
  private JScrollPane gamesListScrollPane; //<-- the scroll pane for the list of game types
  private JLabel pass;
  private JTextField password;
  private JButton createGame, cancel;

  /**
   * Creates the gui component for when the server can not be contacted
   */
  public PICAIUClientGUINewGame(Container frameIn, PICAIUClientGUI parentIn) {
    frame = frameIn;
    parent = parentIn;
    initGUI();
  }

  /**
   * creates the gui components
   */
  public void initGUI() {
    content = new JPanel(new BorderLayout());

    myInfoCont = new JPanel();
    myInfoCont.setMinimumSize(new java.awt.Dimension(245, 60));
    myInfoCont.setPreferredSize(new java.awt.Dimension(245, 60));
    myInfoCont.setMaximumSize(new java.awt.Dimension(245, 60));
    myInfoCont.setSize(new java.awt.Dimension(245, 60));
    myInfo = new JLabel();
    myInfoCont.add(myInfo);

    games = new JPanel();
    gamesList = new JList(parent.getGameTypesModel());
    gamesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    gamesList.addMouseListener(new MouseListener() {

      public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2 && e.getButton() == 1) {
          String amount = "" + gamesList.getSelectedValue();
          amount = amount.replaceAll(",","");
          parent.getClient().sendDataToServer("CREATEGAME#" + amount.substring(1));
          createGame.setEnabled(false);
        }
      }

      public void mousePressed(MouseEvent e) {
      }

      public void mouseReleased(MouseEvent e) {
      }

      public void mouseEntered(MouseEvent e) {
      }

      public void mouseExited(MouseEvent e) {
      }
    });
    gamesListScrollPane = new JScrollPane(gamesList);
    gamesListScrollPane.setMinimumSize(new java.awt.Dimension(245, 320));
    gamesListScrollPane.setPreferredSize(new java.awt.Dimension(245, 320));
    gamesListScrollPane.setMaximumSize(new java.awt.Dimension(245, 320));
    gamesListScrollPane.setSize(new java.awt.Dimension(245, 320));
    games.add(gamesListScrollPane);


    pass = new JLabel("Password:");
    password = new JTextField();
    password.setMinimumSize(new java.awt.Dimension(152, 20));
    password.setPreferredSize(new java.awt.Dimension(152, 20));
    password.setMaximumSize(new java.awt.Dimension(152, 20));
    password.setSize(new java.awt.Dimension(152, 20));

    padding = new JPanel();
    padding.setMinimumSize(new java.awt.Dimension(780, 4));
    padding.setPreferredSize(new java.awt.Dimension(780, 4));
    padding.setMaximumSize(new java.awt.Dimension(780, 4));
    padding.setSize(new java.awt.Dimension(780, 4));


    buttonPanel = new JPanel();
    buttonPanel.setMinimumSize(new java.awt.Dimension(245, 80));
    buttonPanel.setPreferredSize(new java.awt.Dimension(245, 80));
    buttonPanel.setMaximumSize(new java.awt.Dimension(245, 80));
    buttonPanel.setSize(new java.awt.Dimension(245, 80));

    createGame = new JButton(" Start Game ");
    createGame.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        //parent.setGUI("newgame");
        String amount = "" + ((String) gamesList.getSelectedValue()).substring(1);
        amount = amount.replaceAll(",","");
        parent.getClient().sendDataToServer("CREATEGAME#" + amount + "#" + password.getText());
        createGame.setEnabled(false);
      }
    });

    cancel = new JButton(" Cancel ");
    cancel.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        parent.setGUI("lobby");
        parent.getClient().requestGamesList();
      }
    });

    buttonPanel.add(pass);
    buttonPanel.add(password);
    buttonPanel.add(padding);
    buttonPanel.add(createGame);
    buttonPanel.add(cancel);

    content.add(myInfoCont, BorderLayout.NORTH);
    content.add(games, BorderLayout.CENTER);
    content.add(buttonPanel, BorderLayout.SOUTH);

  }

  /**
   * This method switches the clients main container to this GUI's graphics.
   */
  @Override
  public void setAsGUI() {

    frame.removeAll();
    createGame.setEnabled(false);
    frame.add(content, BorderLayout.CENTER);
    frame.validate();

    parent.getClient().sendDataToServer("TYPES");
  }

  /**
   * This sets the colors of all the components affected by the color scheme to that of the current
   * colors for the GUI
   */
  @Override
  public void setColors(PICAIUGUIScheme colorScheme) {

    Color colorBG01 = colorScheme.getBG01();
    Color colorBG02 = colorScheme.getBG02();
    Color colorFG01 = colorScheme.getFG01();
    Color colorBorder01 = colorScheme.getBorder01();

    content.setBackground(colorBG01);
    content.setForeground(colorFG01);
    myInfo.setBackground(colorBG01);
    myInfo.setForeground(colorFG01);
    myInfoCont.setBackground(colorBG01);
    myInfoCont.setForeground(colorFG01);
    games.setBackground(colorBG01);
    games.setForeground(colorFG01);
    games.setBackground(colorBG01);
    games.setForeground(colorFG01);



    pass.setBackground(colorBG02);
    pass.setForeground(colorFG01);
    password.setBackground(colorBG02);
    password.setForeground(colorFG01);
    password.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    padding.setBackground(colorBG01);
    padding.setForeground(colorFG01);
    createGame.setBackground(colorBG01);
    createGame.setForeground(colorFG01);
    createGame.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    cancel.setBackground(colorBG01);
    cancel.setForeground(colorFG01);
    cancel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
    buttonPanel.setBackground(colorBG01);
    buttonPanel.setForeground(colorFG01);


    gamesList.setBackground(colorBG02);
    gamesList.setForeground(colorFG01);
    gamesListScrollPane.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, colorBorder01));
  }

  void setMyInfo(String string) {
    myInfo.setText("[" + string + "]");
  }

  public void enableGameCreation() {
    createGame.setEnabled(true);
  }

  protected JList getGamesList() {
    return gamesList;
  }

  protected JButton getCreateGameButton() {
    return createGame;
  }


  /**
  * This method revalidates all components which may have been updated.
  */
  public void validate(){
    this.gamesListScrollPane.validate();
    this.gamesList.validate();
    this.games.validate();
  }
}
